package rsschart;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class BuildFeed extends HttpServlet {

	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		int setCount;
		int pointCount;
		StringBuffer valueStringStub = new StringBuffer(20);
		
		//-- short cutting: we shouldn't have to validate these...they should come from a drop down:
		setCount = Integer.parseInt(request.getParameter("numberSets"));
		pointCount = Integer.parseInt(request.getParameter("numberPoints"));
			
		request.setAttribute("setCount", setCount);
		request.setAttribute("pointCount", pointCount);
		
		request.setAttribute("chartTitle", "Enter Chart Title");

		//-- build the stub string based on number of points per data set:
		for (int i=0;i<pointCount;i++) {
			valueStringStub.append("?");
			if(i<pointCount-1) {
				valueStringStub.append(":");
			}
		}
				
		//-- this is an easy but heavy way to pass a loop counter to the target jsp:
		//List counter = new ArrayList(setCount);
		List dataSetMaps = new ArrayList();
		Map dataSetMap;
		
		String dataSetVals[] = new String[setCount];
		
		for (int i=0; i<setCount; i++) {
			dataSetMap = new HashMap();
			dataSetMap.put("idx", new Integer(i+1).toString());
			dataSetMap.put("name", "?");
			dataSetMap.put("val", valueStringStub.toString());
			
			dataSetMaps.add(dataSetMap);
		}

		request.setAttribute("dataSetMaps", dataSetMaps);
		//request.setAttribute("stubString", valueStringStub.toString());
		request.setAttribute("oneParam", "?");
		
		request.setAttribute("dataSetsName", "?");
		request.setAttribute("dataSetsValueLabel", "?");
		
		request.setAttribute("catgName", "?");
		request.setAttribute("catgVals", valueStringStub.toString());
		
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/content.jsp");
		dispatcher.forward(request, response);
	}

}
